package pad.com.haidiyun.www.boothprint.printutil;

import android.content.Context;

import org.json.JSONArray;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import pad.com.haidiyun.www.common.Common;
import pad.stand.com.haidiyun.www.R;
import pad.stand.com.haidiyun.www.common.P;
import pad.stand.com.haidiyun.www.common.SharedUtils;


/**
 * 测试数据生成器
 * Created by liuguirong on 8/1/17.
 */

public class PrintOrderDataMaker implements PrintDataMaker {


    private String qr, dataArray;
    private int width;
    private int height;
    private double total;
    Context btService;
    private String remark = "微点筷客推出了餐厅管理系统，可用手机快速接单（来自客户的预订订单），进行订单管理、后厨管理等管理餐厅。";
    private SharedUtils sharedUtils;

    public PrintOrderDataMaker(Context btService, String qr, int width, int height) {
        this.qr = qr;
        this.width = width;
        this.height = height;
        this.btService = btService;
    }

    public PrintOrderDataMaker(Context btService, String qr, int width, int height, String data, double total) {
        this.qr = qr;
        this.width = width;
        this.height = height;
        this.btService = btService;
        this.dataArray = data;
        this.total = total;
        sharedUtils = new SharedUtils(Common.CONFIG);
    }


    @Override
    public List<byte[]> getPrintData(int type) {
        ArrayList<byte[]> data = new ArrayList<>();

        try {
            JSONArray dataAll = new JSONArray(dataArray);
            P.c("已下单数据" + dataAll.toString());
            PrinterWriter printer;
            printer = type == PrinterWriter58mm.TYPE_58 ? new PrinterWriter58mm(height, width) : new PrinterWriter80mm(height, width);
            printer.setAlignCenter();
            data.add(printer.getDataAndReset());

            ArrayList<byte[]> image1 = printer.getImageByte(btService.getResources(), R.drawable.demo);

            data.addAll(image1);

            printer.setAlignLeft();
            printer.printLine();
            printer.printLineFeed();

            printer.printLineFeed();
            printer.setAlignCenter();
            printer.setEmphasizedOn();
            printer.setFontSize(1);
            printer.print("   名典咖啡");
            printer.printLineFeed();
            printer.setEmphasizedOff();
            printer.printLineFeed();

            printer.printLineFeed();
            printer.setFontSize(0);
            printer.setAlignCenter();
            printer.print("订单编号：" + sharedUtils.getStringValue("billId"));
            printer.printLineFeed();

            printer.setAlignCenter();
            printer.print(new SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.getDefault())
                    .format(new Date(System.currentTimeMillis())));
            printer.printLineFeed();
            printer.printLine();

            printer.printLineFeed();
            printer.setAlignLeft();
            printer.print("订单状态: " + "已下单");
            printer.printLineFeed();
//            printer.print("用户昵称: " + sharedUtils.getStringValue("table_code"));
//            printer.printLineFeed();
            printer.print("用餐桌号: " + sharedUtils.getStringValue("table_code") + "号桌");
            printer.printLineFeed();
            printer.print("用餐人数: " + sharedUtils.getIntValue("person"));
            printer.printLineFeed();
            printer.print("预定时间：" + new SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.getDefault())
                    .format(new Date(System.currentTimeMillis())));
            printer.printLineFeed();
//            printer.print("预留时间：30分钟");
//            printer.printLineFeed();
//            printer.print("联系方式：" + "400-775-1598");
//            printer.printLineFeed();
            printer.printLine();
            printer.printLineFeed();

            printer.setAlignLeft();
            printer.print("备注： " + "清淡口味");
            printer.printLineFeed();
            printer.printLine();

            printer.printLineFeed();

            printer.setAlignCenter();
            printer.print("菜品信息");
            printer.printLineFeed();
            //新加
            printer.printLineFeed();
            printer.setAlignCenter();
            printer.printInOneLine("菜名", "单价", 0);
            printer.printLineFeed();
            for (int j = 0; j < dataAll.length(); j++) {
                String name = dataAll.getJSONObject(j).getString("MenuName");
                int number = dataAll.getJSONObject(j).getInt("Number");
                double price = dataAll.getJSONObject(j).getDouble("Price");
                printer.printInOneLine(name + "  X" + number, "￥" + price, 0);
                printer.printLineFeed();
            }
            /*for (int i = 0; i < 3; i++) {

                printer.printInOneLine("干锅包菜", "X" + 3, "￥" + 30, 0);
                printer.printLineFeed();
            }*/
            printer.printLineFeed();
            printer.printLine();
            printer.printLineFeed();
            printer.setAlignLeft();
            printer.printInOneLine("菜品总额：", "￥" + total, 0);


            printer.setAlignLeft();
            printer.printInOneLine("优惠金额：", "￥" + "0.00"
                    , 0);
            printer.printLineFeed();

            printer.setAlignLeft();
            printer.printInOneLine("订金/退款：", "￥" + "0.00"
                    , 0);
            printer.printLineFeed();


            printer.setAlignLeft();
            printer.printInOneLine("总计金额：", "￥" + total, 0);
            printer.printLineFeed();

            printer.printLine();
            printer.printLineFeed();
            printer.setAlignCenter();
            printer.print("谢谢惠顾，欢迎再次光临！");
            printer.print("");
            printer.printLineFeed();
            printer.print("");
            printer.printLineFeed();
            printer.printLineFeed();
            printer.feedPaperCutPartial();
            //新加
            printer.setEmphasizedOn();
            data.add(printer.getDataAndClose());
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


}
